package cn.lds.j368.dsedu.service.impl.impl;

import cn.lds.j368.dsedu.dto.ResultJson;
import cn.lds.j368.dsedu.entity.Video;
import cn.lds.j368.dsedu.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@Service
public class VideoServiceImpl implements VideoService {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;

    @Override
    public ResultJson<Video> selectVideoById(Integer videoId) {

//        ResultJson<Video> result = restTemplate.getForObject("http://localhost:8888/api/video/" + videoId, ResultJson.class);

//        List<ServiceInstance> instances = discoveryClient.getInstances("ds-edu-video-service");
//
//        ServiceInstance serviceInstance = instances.get(0);
//
//        String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/api/video/" + videoId;

        String url = "http://ds-edu-video-service/api/video/" + videoId;

//        System.out.println(url);

        ResultJson result = restTemplate.getForObject(url, ResultJson.class);

        return result;
    }
}
